package com.handinfo.android.uicontrols;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.game.Role;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.ui.window.UIFamilyWindow;
import com.handinfo.android.ui.window.UIGetMoney;
import com.handinfo.android.ui.window.UIRankWindow;
import com.handinfo.android.ui.window.UIRenWuWindow;
import com.handinfo.android.ui.window.UISocial;
import com.handinfo.android.uicontrols.controls.DWControl;
import com.handinfo.android.utils.Tools;

/**
 * 用户引导
 * @author dingjian
 */
public class DWGuideAttention {
	public CopyOnWriteArrayList<DWGuideObject> m_guide_list = new CopyOnWriteArrayList<DWGuideObject>();
	public int m_guide_index = 0;
	public DWControl m_parent = null;//前一个控件
	/**
	 * 读取一个用户引导
	 * @param dis
	 * @throws IOException
	 */
	public void recvGuide(DataInputStream dis) throws IOException {
		Tools.debugPrintln("读取用户引导开始!");
		m_guide_list.clear();
		if (m_parent != null) {
			m_parent.closeGuide();
		}
		m_guide_index = 0;
		byte size = dis.readByte();
		for (byte i = 0; i < size; i++) {
			DWGuideObject guide = new DWGuideObject();
			guide.m_interface = dis.readUTF();
			Tools.debugPrintln("界面 : " + guide.m_interface);
			guide.m_element = dis.readUTF();
			Tools.debugPrintln("元素 : " + guide.m_element);
			guide.m_direct = dis.readByte();
			Tools.debugPrintln("方向 : " + guide.m_direct);
			guide.m_message = dis.readUTF();
			Tools.debugPrintln("信息 : " + guide.m_message);
			guide.m_wait_next = dis.readByte() / 10 * DWGameManager.FRAME_PER_SECOND;
			Tools.debugPrintln("下次等待时间 : " + guide.m_wait_next);
			if (i == 0 && guide.m_wait_next <= 0) {
				m_parent = guide.setGuide();
			}
			guide.m_amin_index = dis.readByte();
//			if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
//				guide.m_amin_index = 1;
//			}
			guide.m_anim = null;
			Tools.debugPrintln("指引动画框 : " + guide.m_amin_index);
			m_guide_list.add(guide);
		}
		Tools.debugPrintln("读取用户引导结束!");
		
//		DWGuideObject guide = new DWGuideObject();
//		guide.m_interface = "main";
//		guide.m_element = "skill1";
//		guide.m_direct = 0;
//		guide.m_message = "skill1";
//		m_parent = guide.setGuide();
//		m_guide_list.add(guide);
//		
//		guide = new DWGuideObject();
//		guide.m_interface = "main";
//		guide.m_element = "skill2";
//		guide.m_direct = 0;
//		guide.m_message = "skill2";
//		m_guide_list.add(guide);
//		
//		guide = new DWGuideObject();
//		guide.m_interface = "main";
//		guide.m_element = "skill3";
//		guide.m_direct = 0;
//		guide.m_message = "skill3";
//		m_guide_list.add(guide);
//		
//		guide = new DWGuideObject();
//		guide.m_interface = "main";
//		guide.m_element = "auto";
//		guide.m_direct = 0;
//		guide.m_message = "auto";
//		m_guide_list.add(guide);
	}
	
	public DWGuideObject getCurrentGuideObject() {
		if (m_guide_list.size() > 0 && m_guide_index > -1 && m_guide_index < m_guide_list.size()) {
			return m_guide_list.get(m_guide_index);
		}
		return null;
	}
	
	public DWGuideObject getCurrentGuideObjects() {
		if ( m_guide_index -1 >= 0 && m_guide_index-1 < m_guide_list.size()) {
			return m_guide_list.get(m_guide_index-1);
		}
		return null;
	}
	
	public DWGuideObject getGuideObject(int index) {
		if (m_guide_list.size() > 0 && index > -1 && index < m_guide_list.size()) {
			return m_guide_list.get(index);
		}
		return null;
	}
	
	
	
	public void waitGuide() {
		DWGuideObject guide = getCurrentGuideObject();
		if (guide != null) {
			if (guide.m_wait_next > 0) {//下一次出现时间
				guide.m_wait_next--;
				if (guide.m_wait_next <= 0) {
					m_parent = guide.setGuide();
					guide.m_wait_time = 0;
				}
			} else {
				if (guide.m_waiting) {//保留时间
					m_parent = guide.setGuide();
					if (++guide.m_wait_time > 300 || m_parent != null) {
						guide.m_wait_time = 0;
						guide.m_waiting = false;
					}
				}
			}
			
		}
		
	}
	
	public boolean nextFrame() {
		if (m_parent != null) {
			m_parent.closeGuide();
		}
		if (m_guide_index < m_guide_list.size() - 1) {
			m_guide_index++;
			DWGuideObject guide = getCurrentGuideObject();
			if (guide.m_wait_next > 0) {
			} else {
				if (guide != null) {
					m_parent = guide.setGuide();
					guide.m_wait_time = 0;
				}
			}
			return true;
		}
		return false;
	}
	
	/**
	 * 开启界面
	 * @param inter 界面
	 * @param element 元素
	 */
	public static void openInterface(String inter, String element) {
		Tools.debugPrintln("打开界面 : " + inter + " : " + element);
		if (inter != null && !inter.equals("")) {
			Role role = DWGameManager.getInstance().m_role;
			if (inter.equals(DWGuideObject.INTERFACE_TREE)) {//摇钱树
				role = DWGameManager.getInstance().m_role;
				if (role.m_lv < 5) {
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "还未达到开启等级");
					return;
				}
				DWGameManager.getInstance().getSendMessage().sendYQSInto(DWGameManager.getInstance().m_role.m_gameId);
				DWGameManager.getInstance().getSendMessage().sendYQSFriendList(UISocial.SOCIAL_TYPE_FRIEND, (byte) 0, UIGetMoney.num);
			} else if (inter.equals(DWGuideObject.INTERFACE_TEMPLE)) {//祭天
				if (role.m_lv < 15) {
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "还未达到开启等级");
					return;
				}
				UIWindows.getInstance().m_jiming.open((byte) 0);
			} else if (inter.equals(DWGuideObject.INTERFACE_DRUG)) {//炼丹
				if (role.m_lv < 25) {
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "还未达到开启等级");
					return;
				}
				DWGameManager.getInstance().getSendMessage().sendNeiDangOpenWindow();
			} else if (inter.equals(DWGuideObject.INTERFACE_PET)) {//宠物
				DWGameManager.getInstance().getSendMessage().sendChongWuOpenWin();
			} else if (inter.equals(DWGuideObject.INTERFACE_FAMILY)) {//家族
				UIWindows.getInstance().m_family.open((byte)0);
			} else if (inter.equals(DWGuideObject.INTERFACE_SOCIAL)) {//社交
				DWGameManager.getInstance().getSendMessage().sendSocialList(UISocial.SOCIAL_TYPE_FRIEND, 0, UISocial.num);
			} else if (inter.equals(DWGuideObject.INTERFACE_FORGE)) {//工坊
				if (role.m_lv < 10) {
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "还未达到开启等级");
					return;
				}
				UIWindows.getInstance().m_tiangong.open((byte) 0);
			} else if (inter.equals(DWGuideObject.INTERFACE_INSTANCE)) {
				role = DWGameManager.getInstance().m_role;
				if (role.m_lv < 15) {
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "还未达到开启等级");
					return;
				}
				DWGameManager.getInstance().getSendMessage().sendGameCarbonOpen();
			} else if (inter.equals(DWGuideObject.INTERFACE_MAIN_KEJIE)) {
				UIRenWuWindow renwu = UIWindows.getInstance().m_renwu;
				renwu.RenWu();
				UIWindows.getInstance().m_renwu.KeJie();
			} else if (inter.equals(DWGuideObject.INTERFACE_FORGE_XIANGQIAN)) {
				if (role.m_lv < 10) {
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "还未达到开启等级");
					return;
				}
				UIWindows.getInstance().m_tiangong.open((byte) 0);
				UIWindows.getInstance().m_tiangong.qiehuan_xiangqian();
			} else if (inter.equals(DWGuideObject.INTERFACE_FORGE_QIANGHUA)) {
				if (role.m_lv < 10) {
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "还未达到开启等级");
					return;
				}
				UIWindows.getInstance().m_tiangong.open((byte) 0);
				UIWindows.getInstance().m_tiangong.qiehuan_qianghua();
			} else if (inter.equals(DWGuideObject.INTERFACE_FAMILY_QIFU)) {
				if (role.m_family_name != null && !role.m_family_name.equals("")) {
					UIFamilyWindow fam = UIWindows.getInstance().m_family;
					fam.open((byte)1);
				} else {
					DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "您没有家族,无法参与祈福!");
				}
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_DENGJI)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.等级);
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_JIAZU)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.家族);
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_CAIFU)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.财富);
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_JISHA)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.击杀);
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_GONGXIAN)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.贡献);
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_ZHANGONG)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.战功);
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_SHEJIAO)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.社交);
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_GERENJINGJI)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.个人竞技);
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_JIAZUJINGJI)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.家族竞技);
			} else if (inter.equals(DWGuideObject.INTERFACE_RANKING_HUNYIN)) {
				UIRankWindow rank = UIWindows.getInstance().m_rank;
				rank.Rank(UIRankWindow.婚姻);
			}
		}
	}
	
}
